<?php
/***/
namespace app\admin\controller;

use app\admin\model\Channel;
use app\admin\model\mainRecordOpe;
use app\admin\model\RecordM;

/**
 * 充值流水账
 * @Author    lf
 * @DateTime  2018-06-29T13:52:40+0800
 */
class Record extends Father {
	/**
	 * 首页
	 * @Author   lf
	 * @DateTime 2018-06-29T13:52:40+0800
	 * @return   [type]                   [description]
	 */
	public function index($page = 1) {
		//总的
		$field_list = [
			['field' => 'numbers', 'type' => 'numbers', 'title' => '序号']
			, ['field' => 'channel_id', 'title' => '用户ID']
			, ['field' => 'company_name', 'title' => '公司名称', 'minWidth' => 150]
			, ['field' => 'note_num_ago', 'title' => '调整前数目(条)', 'minWidth' => 130]
			, ['field' => 'note_num_now', 'title' => '调整后数目(条)', 'minWidth' => 130]
			, ['field' => 'ope_type', 'title' => '操作类型', 'minWidth' => 100]
			, ['field' => 'operation_num', 'title' => '操作数目(条)', 'minWidth' => 130]
			, ['field' => 'operation_fee', 'title' => '操作金额(元)', 'minWidth' => 130]
			, ['field' => 'account_balance', 'title' => '余额', 'minWidth' => 130]
			, ['field' => 'remarks', 'title' => '备注', 'minWidth' => 150, 'align' => 'center']
			, ['field' => 'username', 'title' => '操作人', 'minWidth' => 100, 'align' => 'center']
			, ['field' => 'operation_time', 'title' => '操作时间', 'minWidth' => 175, 'align' => 'center']
			, ['field' => 'right', 'fixed' => 'right', 'title' => '操作', 'toolbar' => '#barDemo', 'width' => 60, 'unresize' => 'true'],
		];

		//总的列
		$field_list_column = array_column($field_list, 'field');
		//可用的列
		$table_list = db('adm_role_table_list')->join("adm_table_list", "adm_role_table_list.table_field_id=adm_table_list.id")->where('role_id', session('role_id'))->where('adm_table_list.auth_id', 50)->column('adm_table_list.table_field');
		foreach ($field_list_column as $key => $value) {
			//隐藏列
			if (!in_array($value, $table_list)) {
				unset($field_list[$key]);
			}
		}

		$onlyrequest = [['st', date('Y-m-d 00:00:00')], ['et', date('Y-m-d H:i:s')], ['oty', ''], ['channel_id', ''], ['page', 1], ['limit', 15]];
		$request     = $this->getRequests($onlyrequest, 'GET');

		if (input('get.api')) {

			$recordm = new RecordM;

			$data = $recordm->getRecordAll($request);

			$count = $data->total();

			$list = $data->toArray();

			$list = $list['data'];

			return json(['code' => 0, 'msg' => '', 'count' => $count, 'data' => $list]);

		} else {

			if (is_OneCha()) {

				$channel_data = Channel::field('channel_id,company_name')->where('p_id', session('relate_id'))->whereor('channel_id', session('relate_id'))->select();
			} else {

				$channel_data = Channel::field('channel_id,company_name')->select();
			}
			$this->assign('channel_data', $channel_data);
			$this->assign('requests', $request);
			$this->assign('recordope', mainRecordOpe::all());
			$this->assign('field_list', json_encode($field_list));
			return $this->fetch();
		}
	}

	/**
	 * 更新备注
	 * @Author   lf
	 * @DateTime 2018-06-29T15:01:24+0800
	 * @return   [type]                   [description]
	 */
	public function remarks_up() {
		if (request()->has('remarks', 'post')) {

			$record_id = trim(input('post.id'));
			$remarks   = trim(input('post.remarks'));

			if (empty($record_id)) {

				echo "参数失效";return;
			}

			try {

				$recordm          = RecordM::get($record_id);
				$recordm->remarks = $remarks;
				$recordm->isUpdate(true)->save();
				echo "success";
			} catch (Exception $e) {

				echo "服务繁忙";
			}
		} else {

			$id   = input('get.id');
			$info = RecordM::get($id);
			$this->assign('info', $info);
			return $this->fetch();
		}
	}

}